<template>
  <PanelHeader :route="route"></PanelHeader>
  <div class="btns">
    <el-button @click="open(null)" type="primary" icon="Plus" size="small">新增</el-button>
  </div>
  <el-table :data="tableData.list" style="width: 100%;">
    <el-table-column prop="id" label="id"> </el-table-column>
    <el-table-column prop="name" label="昵称"> </el-table-column>
    <el-table-column prop="permissionName" label="菜单权限" width="500px"> </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button type="primary" @click="open(scope.row)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <div class="pagination-info">
    <el-pagination v-model:current-page="pagiantionData.pageNum" :page-size="pagiantionData.pageSize"
      :background="background" layout="total, prev, pager, next" :total="tableData.total" size="small"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>
  <el-dialog :before-close="beforeClose" v-model="dialogFormVisable" title="添加权限" width="500">
    <el-form ref="formRef" label-width="100px" label-position="left" :model="form" :rules="rules">
      <el-form-item v-show="false" prop="id">
        <el-input v-model="form.id"></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="请填写权限名称"></el-input>
      </el-form-item>
      <el-form-item label="权限" prop="permission">
        <el-tree :data="permissionData" style="max-width: 600px;" node-key="id" show-checkbox
          :default-checked-keys="defaultKeys" ref="treeRef" :default-expanded-keys="[2]">
        </el-tree>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="confirm(formRef)">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, onMounted, nextTick } from 'vue';
import { userGetmenu, userSetmenu, menuList } from '../../../api/index'
import PanelHeader from '../../../components/panelHeader.vue';
import { useRoute } from 'vue-router';
const route = useRoute()
//列表数据
const tableData = reactive({
  list: [],
  total: 0
})
//打开弹窗
const open = (rowData = {}) => {
  dialogFormVisable.value = true
  nextTick(() => {
    if (rowData) {
      Object.assign(form, { id: rowData.id, name: rowData.name })
      treeRef.value.setCheckedKeys(rowData.permission)
    }
  })
}
const pagiantionData = reactive({
  pageNum: 1,
  pageSize: 10
})
onMounted(() => {
  userGetmenu().then(({ data }) => {
    permissionData.value = data.data
  })
  getList()
})
const handleSizeChange = (val) => {
  pagiantionData.pageSize = val
  getList()
}
const handleCurrentChange = (val) => {
  pagiantionData.pageNum = val
  getList()
}
//请求列表数据
const getList = () => {
  menuList(pagiantionData).then(({ data }) => {
    console.log(data);
    const { list, total } = data.data
    tableData.list = list
    tableData.total = total
  })
}
const form = reactive({
  id: '',
  name: '',
  permission: '',
})
//树形菜单权限数据
const permissionData = ref([])
const dialogFormVisable = ref(false)
const beforeClose = () => {
  dialogFormVisable.value = false
  Object.assign(form, {
    id: '',
    name: '',
    permission: '',
  })
}
//选中权限
const defaultKeys = [4, 5]
const treeRef = ref()
const formRef = ref()
const rules = reactive({
  name: [{ required: true, trigger: 'blur', message: '请输入权限名称' }]
})
//表单提交
const confirm = async (formRef) => {
  if (!formRef) return
  await formRef.validate((valid, fields) => {
    if (valid) {
      const permissions = JSON.stringify(treeRef.value.getCheckedKeys())
      userSetmenu({ name: form.name, permissions, id: form.id }).then(({ data }) => {
        console.log(data);
        beforeClose()
        getList()
      })
    }
    else {
      console.log("error");
    }
  })
}
</script>

<style lang="less" scoped>
.btns {
  padding: 10px 0 10px 10px;
  background-color: #fff;
}
</style>
